package com.wdj.library.service.impl;

import com.wdj.library.dao.UserInfoDao;
import com.wdj.library.entity.UserInfo;
import com.wdj.library.entity.vo.Result;
import com.wdj.library.entity.vo.UserInfoVO;
import com.wdj.library.service.UserInfoService;
import com.wdj.library.util.AESUtil;
import com.wdj.library.util.JwtUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * (UserInfo)表服务实现类
 *
 * @author wdj
 * @since 2020-08-12 11:54:09
 */
@Service("userInfoService")
public class UserInfoServiceImpl implements UserInfoService {
    @Resource
    private UserInfoDao userInfoDao;

    @Override
    public Result login(String account, String password) {
        UserInfo userInfo = userInfoDao.queryByAccount(account);
        if(userInfo == null){
            return Result.error("用户不存在");
        }else{
            try {
                String result = AESUtil.decryptByAES(userInfo.getPassword());
                if(result.equals(password)){
                    //  生成签名
                    String token = JwtUtil.sign(userInfo.getId().toString());
                    UserInfoVO userInfoVO = new UserInfoVO();
                    BeanUtils.copyProperties(userInfo, userInfoVO);
                    userInfoVO.setToken(token);
                    return Result.success(userInfoVO);
                }else{
                    return Result.error("密码错误");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        return null;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public UserInfo queryById(Integer id) {
        return this.userInfoDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<UserInfo> queryAllByLimit(int offset, int limit) {
        return this.userInfoDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param userInfo 实例对象
     * @return 实例对象
     */
    @Override
    public Result insert(UserInfo userInfo) {
        String password = null;
        try {
            password = AESUtil.encryptByAES(userInfo.getPassword());
        } catch (Exception e) {
            e.printStackTrace();
        }
        userInfo.setPassword(password);
        userInfo.setCreateTime(new Date());
        userInfo.setUpdateTime(new Date());
        int num = this.userInfoDao.insert(userInfo);
        if(num == 1){
            return Result.success();
        }else {
            return Result.error("创建失败");
        }

    }

    /**
     * 修改数据
     *
     * @param userInfo 实例对象
     * @return 实例对象
     */
    @Override
    public UserInfo update(UserInfo userInfo) {
        this.userInfoDao.update(userInfo);
        return this.queryById(userInfo.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.userInfoDao.deleteById(id) > 0;
    }
}